<template>
  <el-card class="form-container" shadow="never">
    <el-form :model="heat" :rules="rules" ref="heatFrom" label-width="240px">
      <el-form-item label="手机名称：" prop="productName">
        <el-input v-model="heat.productName"></el-input>
      </el-form-item>
      <el-form-item label="20款应用两轮打开时间，单位：s">
        <el-input v-model="heat.openTime"></el-input>
      </el-form-item>
      <el-form-item label="半小时原神最高画质机身最高温度，单位：℃">
        <el-input v-model="heat.genshinHeat"></el-input>
      </el-form-item>
      <el-form-item label="一小时和平精英最高画质机身最高温度，单位：℃">
        <el-input v-model="heat.hpHeat"></el-input>
      </el-form-item>
    
      <el-form-item>
        <el-button type="primary" @click="onSubmit('heatFrom')">提交</el-button>
        <el-button v-if="!isEdit" @click="resetForm('heatFrom')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
  import {createHeat, getHeat, updateHeat} from '@/api/heat'
  import SingleUpload from '@/components/Upload/singleUpload'
  const defaultHeat={
    productName: '',
    openTime: 0,
    genshinHea: 0,
    hpHeat: 0
  };
  export default {
    name: 'HeatDetail',
    components:{SingleUpload},
    props: {
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        heat:Object.assign({}, defaultHeat),
        rules: {
          productName: [
            {required: true, message: '请输入手机名称', trigger: 'blur'},
            {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
          ]
        }
      }
    },
    created() {
      if (this.isEdit) {
        getHeat(this.$route.query.id).then(response => {
          this.heat = response.data;
        });
      }else{
        this.heat = Object.assign({},defaultHeat);
      }
    },
    methods: {
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('是否提交数据', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              if (this.isEdit) {
                updateHeat(this.$route.query.id, this.heat).then(response => {
                  this.$refs[formName].resetFields();
                  this.$message({
                    message: '修改成功',
                    type: 'success',
                    duration:1000
                  });
                  this.$router.back();
                });
              } else {
                createHeat(this.heat).then(response => {
                  this.$refs[formName].resetFields();
                  this.heat = Object.assign({},defaultHeat);
                  this.$message({
                    message: '提交成功',
                    type: 'success',
                    duration:1000
                  });
                });
              }
            });

          } else {
            this.$message({
              message: '验证失败',
              type: 'error',
              duration:1000
            });
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.heat = Object.assign({},defaultHeat);
      }
    }
  }
</script>
<style>
</style>


